<?php

require_once( '../../libs/smarty.inc' );

require_once( '../../libs/smt_variables.php' );

require_once( '../../libs/Class_DB.php' );

require_once( '../../libs/Class_ERROR.php' );

require_once( '../../libs/login.inc.php' );

require_once( '../../libs/const.php' );



$forms = $_GET;

$db = new Class_DB;



$arr_result[''] = '';



$sql = "SELECT * FROM D_SHIPMENT";



/****  キーワード（製品名・納品先・依頼者名 ****/

$where1 = "";

if( $forms['keyword'] != '' ){

	$where1 = "(company LIKE '%" . $db->quote( $forms['keyword'] ) . "%' OR request_name LIKE '%" . $db->quote( $forms['keyword'] ) . "%' OR search_text LIKE '%" . $db->quote( $forms['keyword'] ) . "%') AND ";

}



/****  伝票区分 ****/

$where2 = "";

if( $forms['denpyo'] ){

	$where2 = sprintf("id IN (SELECT id FROM D_SHIPMENT_DETAIL WHERE denpyo = %u) AND ", $forms['denpyo'] );

}



/****  フォロー記入 ****/

$where3 = ""; $where4 = "";

if( isset( $forms['follow'] ) ){

	if( !(in_array( 1, $forms['follow'] ) && in_array( 2, $forms['follow'] ) ) ){

		if( in_array( 1, $forms['follow'] ) ){

			$where3 = "(follow_text1 <> '' OR follow_text2 <> '') AND ";

		}

		if( in_array( 2, $forms['follow'] ) ){

			$where4 = "(follow_text1 = '' AND follow_text2 = '') AND ";

		}

	}

}



/**** 登録日 ****/

$where5 = ""; $where6 = "";

if( $forms['date1'] != '' ){

	$where5 = sprintf("r_date >= '%s 00:00:00' AND ", $forms['date1'] );

}

if( $forms['date2'] != '' ){

	$where6 = sprintf("r_date <= '%s 23:59:59' AND ", $forms['date2'] );

}



if( $where1 . $where2 . $where3 . $where4 . $where5 . $where6 != "" ){

	$sql .= " WHERE ";

	$sql .= $where1 . $where2 . $where3 . $where4 . $where5 . $where6;



	$sql = substr( $sql, 0, strlen($sql) -5 );

}



$db->query( $sql );

$id = array();

while( $row = $db->fetch() ){

	$id[] = $row['id'];

}



if( !count( $id ) ) exit;



$head = "依頼No.,登録日時,更新日時,依頼者名,製品番号/品名,伝票区分,数量,材料費,材料費合計,目的/備考,会社名,部署,氏名,希望納期,備考,フォロー,フォロー1日付,フォロー1内容,フォロー2日付,フォロー2内容";



$str_id = implode( $id, ",");



$sql = sprintf( "SELECT * FROM D_SHIPMENT_DETAIL A, D_SHIPMENT B WHERE A.id IN ( %s ) AND A.id = B.id", $str_id );

$db->query( $sql );



$data = array();



while( $row = $db->fetch() ){

	

	$row['id'] = 'B' . sprintf('%05d', $row['id']);

	

	$data[] = mb_convert_encoding( sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"",

			$row['id'],

			$row['r_date'],

			$row['u_date'],

			$row['request_name'],

			$row['pro_name'],

			$arr_denpyo[$row['denpyo']],

			$row['amount'],

			$row['cost'],

			$row['cost'] * $row['amount'],

			$row['biko'],

			$row['company'],

			$row['division'],

			$row['cust_name'],

			$row['limit_date'],

			$row['memo'],

			$arr_result[$row['follow_result']],

			$row['follow_date1'],

			$row['follow_text1'],

			$row['follow_date2'],

			$row['follow_text2']

		),

		"sjis",

		"utf8"

	);

}



$filename = date("YmdHis") . '.csv';



$head = mb_convert_encoding( $head, "sjis", "utf8");





header("Content-Type: application/octet-stream");

header('Content-Disposition: attachment; filename=' . $filename );



print $head;

print "\n";

print implode( $data, "\n" );

?>